package com.beep.erp.model.transfer.pallet;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.BaseBean;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.bean.BizStorageLocationsListBean;
import com.beep.erp.common.bean.PalletGoodsBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.SPUtils;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.ClearEditText;
import com.beep.erp.common.widget.popwindow.SelectOneTagPopupWindow;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.tips.TipsHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.beep.erp.AppContext._context;

/**
 * 根据条码查询托盘商品信息
 */
public class PalletGoodsActivity extends ToolbarActivity<PalletGoodsPresenter, PalletGoodsModel> implements PalletGoodsContract.View, RefreshView.OnFreshListener {
    private static final String PALLETID = "name";
    private static final String STATUS = "status";
    @BindView(R.id.appbatlayout)
    AppBarLayout appbatlayout;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_goods_code)
    TextView tvGoodsCode;
    @BindView(R.id.tv_goods_name)
    TextView tvGoodsName;
    @BindView(R.id.tv_goods_date)
    TextView tvGoodsDate;
    @BindView(R.id.tv_goods_number)
    TextView tvGoodsNumber;
    @BindView(R.id.super_refresh)
    RefreshView superRefresh;
    @BindView(R.id.activity_invite)
    CoordinatorLayout activityInvite;
    @BindView(R.id.tv_goods_location)
    TextView tvGoodsLocation;
    @BindView(R.id.tv_select_location)
    TextView tvSelectLocation;
    @BindView(R.id.ll_select_location)
    LinearLayout llSelectLocation;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.et_product_num)
    ClearEditText etProductNum;
    @BindView(R.id.ll_goods_num)
    LinearLayout llGoodsNum;

    private TipsHelper mTipsHelper;
    private String locationId;
    private ArrayList<BizStorageLocationsListBean.BizStorageLocationsBean> mLocationBeen = new ArrayList<>();
    private String status = "";
    private String number;
    private String userId;
    private String name;
    private String goodsId;
    private String storageLocationId;


    public static void start(Context context, String name, String status) {
        Intent intent = new Intent(context, PalletGoodsActivity.class);
        intent.putExtra(PALLETID, name);
        intent.putExtra(STATUS, status);
        context.startActivity(intent);
    }

    @Override
    protected boolean isBack() {
        return true;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_pallet_goods_detail;
    }

    @Override
    protected void initView() {
        status = getIntent().getStringExtra(STATUS);
        if (status.equals("1")) {
            //酱料期出
            getTvTitle().setText("酱料期出");
            llGoodsNum.setVisibility(View.VISIBLE);
        } else if (status.equals("31")) {
            getTvTitle().setText("酱料期入");
            llGoodsNum.setVisibility(View.VISIBLE);
        } else {
            getTvTitle().setText(R.string.goods_detail);
            llSelectLocation.setVisibility(View.VISIBLE);
        }

        mTipsHelper = createTipsHelper(superRefresh);
        tvRight.setText(R.string.save);
        tvRight.setVisibility(View.VISIBLE);
        SPUtils spUtils = new SPUtils(_context, Constants.EXTRA_LOGIN_SP);
        userId = spUtils.getString(Constants.USER_ID);
    }

    @Override
    public void getDate() {
        //请求数据
        getPalletGoodsDetail();
        getBizStorageLocationList();
    }

    private void getBizStorageLocationList() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("type", "0");
        mPresenter.getBizStorageLocationList(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", "0");
    }

    @OnClick({R.id.ll_select_location, R.id.tv_right})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.ll_select_location:
                SelectOneTagPopupWindow selectOneTagPopupWindow = new SelectOneTagPopupWindow(
                        this, tvSelectLocation, "", mLocationBeen, false
                );
                selectOneTagPopupWindow.setOnItemClcik((id, msg) -> {
                    locationId = id;
                    tvSelectLocation.setText(msg);
                });
                break;
            case R.id.tv_right:
                if (ToolsUtils.isEmpty(status)) {
                    doTransfer();
                } else {
                    doEarly();
                }
                break;
        }
    }

    /**
     * 酱料期出保存操作
     */
    private void doEarly() {
        String edNumber = etProductNum.getText().toString();
        if (ToolsUtils.isEmpty(edNumber)) {
            ToastUtils.showShort("请输入商品数量");
            return;
        }
        long mills = TimeUtils.getCurTimeMills();
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("barcode", name);
        map.put("number", edNumber);
        map.put("goodsCode", goodsId);
        map.put("typeInfo", status);
        map.put("manufactureDate", "");
        map.put("storageLocationId", "");

        mPresenter.saveEarlyStoge(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", name, goodsId, edNumber, status, userId, "", "");
    }

    /**
     * 移库保存操作
     */
    private void doTransfer() {
        if (ToolsUtils.isEmpty(locationId)) {
            ToastUtils.showShort("请选择库位");
            return;
        }
        long mills = TimeUtils.getCurTimeMills();
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("barcode", name);
        map.put("goodsId", goodsId);
        map.put("number", number);
        map.put("storageLocationIdFront", storageLocationId);
        map.put("storageLocationIdAfter", locationId);

        mPresenter.movePosition(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "",
                userId, name, goodsId, number, storageLocationId, locationId);
    }


    @Override
    public void getPalletGoodsDetail(PalletGoodsBean palletGoodsBean) {
        name = palletGoodsBean.getName();
        goodsId = palletGoodsBean.getGoodsCode();
        number = palletGoodsBean.getNumber();
        storageLocationId = palletGoodsBean.getStorageLocationId();
        tvName.setText(palletGoodsBean.getName());
        tvGoodsCode.setText(palletGoodsBean.getGoodsCode());
        tvGoodsName.setText(palletGoodsBean.getGoodsName());
        tvGoodsDate.setText(palletGoodsBean.getManufactureDate());
        tvGoodsNumber.setText(palletGoodsBean.getNumber());
        tvGoodsLocation.setText(palletGoodsBean.getStorageLocationName());
    }

    @Override
    public void movePosition(BaseBean baseBean) {
        ToastUtils.showShort(getString(R.string.move_success));
        finish();
    }

    @Override
    public void saveEarlyStoge(BaseBean baseBean) {
        ToastUtils.showShort(getString(R.string.option_success));
        finish();
    }

    @Override
    public void getBizStorageLocationList(BizStorageLocationsListBean bizStorageLocationsListBean) {
        mLocationBeen.addAll(bizStorageLocationsListBean.getBizStorageLocations());
    }

    private void getPalletGoodsDetail() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("name", getIntent().getStringExtra(PALLETID));
        mPresenter.getPalletGoodsDetail(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", getIntent().getStringExtra(PALLETID));
    }

    @Override
    public void showLoading() {
        showWaitDialog(R.string.optoning);
    }

    @Override
    public void hideLoading() {
        hideWaitDialog();
        superRefresh.onFinishFreshAndLoad();
    }

    @Override
    public void showMessage(String message) {
        //错误信息
        if (message.equals(getString(R.string.connection_timed_out)) || message.equals(getString(R.string.error)) || message.equals(getString(R.string.network_error)) || message.equals(getString(R.string.connection_failed)) || message.equals(getString(R.string.certificate_validation_failed))) {
            mTipsHelper.showError(true, message, v -> getDate());
        } else {
            ToastUtils.showShort(message);
        }
    }

    //刷新
    @Override
    public void onRefresh() {
        getDate();
    }

    @Override
    public void onLoadmore() {

    }

}
